﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Runtime;
using Obsidian.Utils;
using Obsidian.Action;
using Obsidian.Web;
using Obsidian.Edm;
using Oak.Base;
using Oak.Model;
using Oak.MagSys.Data;

namespace Oak.MagSys.Logic
{
    public class IconLG : OakLG<ISession>
    {
        public IconLG(ISession session) : base(session) { }

        public List<MsIconInfo> GetIconList(MsIconQry qry, out string msg)
        {
            if (!Session.IsAdmin)
            {
                msg = "当前没有权限执行该操作";
                return null;
            }

            IconDA iconDA = new IconDA();
            List<MsIconInfo> list = iconDA.SelectList(qry);
            msg = "";
            return list;
        }

        public MsIconInfo GetIconById(long iconId, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前没有权限执行该操作";
                return null;
            }

            MsIconInfo icon = new MsIconInfo();
            if (!icon.GetByPrimaryKey(iconId))
            {
                msg = "该图标信息不存在";
                return null;
            }
            msg = "";
            return icon;
        }

        public MsIconInfo AddIcon(MsIconInfo icon, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前没有权限执行该操作";
                return null;
            }

            if (icon.ParentId.IsNull || icon.ParentId.Value < 0)
                icon.ParentId.Set(0);

            long parentId = icon.ParentId.Value;
            if (parentId > 0)
            {
                MsIconInfo parent = this.GetIconById(parentId, out msg);
                if (parent == null)
                {
                    msg = "该父级图标不存在";
                    return null;
                }
                icon.Depth.Set(parent.Depth.Value + 1);
            }
            else
            {
                icon.Depth.Set(0);
            }

            if (icon.SortOrder.IsNull)
                icon.SortOrder.Set(0);

            if (icon.IsSystemIcon.IsNull)
                icon.IsSystemIcon.Set(false);

            if (!VerifyIcon(icon, out msg))
                return null;

            try
            {
                icon.Save();
            }
            catch (Exception ex)
            {
                msg = "保存信息错误";
                return null;
            }

            msg = "";
            return icon;
        }

        public MsIconInfo UpdateIcon(MsIconInfo icon, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前没有权限执行该操作";
                return null;
            }

            if (icon.IconId.IsNull)
            {
                msg = "图标ID不能为空";
                return null;
            }
            MsIconInfo dbIcon = this.GetIconById(icon.IconId.Value, out msg);
            if (dbIcon == null) return null;

            dbIcon.ResetAssigned();
            if (!icon.FrameId.IsNull)
                dbIcon.FrameId.Set(icon.FrameId.Value);
            if (!icon.IconText.IsNull)
                dbIcon.IconText.Set(icon.IconText.Value);
            if (!icon.IconImage.IsNull)
                dbIcon.IconImage.Set(icon.IconImage.Value);
            if (!icon.SortOrder.IsNull)
                dbIcon.SortOrder.Set(icon.SortOrder.Value);

            if (!VerifyIcon(icon, out msg))
                return null;

            try
            {
                dbIcon.Update();
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "保存信息错误:" + code;
                return null;
            }

            msg = "";
            return dbIcon;

        }

        public bool DeleteIcon(long iconId, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前没有权限执行该操作";
                return false;
            }

            MsIconInfo icon = this.GetIconById(iconId, out msg);
            if (icon == null) return false;

            try
            {
                icon.Delete();
                msg = "";
                return true;
            }
            catch (Exception ex)
            {
                msg = "操作失败";
                return false;
            }
        }

        private bool VerifyIcon(MsIconInfo icon, out string msg)
        {

            if (!icon.ParentId.IsNull && icon.ParentId.Value > 0)
            {
                if (icon.FrameId.IsNull)
                {
                    msg = "菜单框架属性不能为空";
                    return false;
                }
                FrameLG frmAct = new FrameLG(Session);
                if (frmAct.GetFrameById(icon.FrameId.Value, out msg) == null)
                    return false;
            }

            msg = "";
            return true;
        }

    }
}
